<?php
namespace Com\Pay;
//yimatong gonzhonghao
class YiMatong
{

    private $config = array();

    public function __construct($mchData)
    {
        $this->config = $mchData;
    }

    /*生成支付连接*/
    public function getUrl($order_info)
    {
        $merchantCode = $this->config['mch_id'];
//        $notifyUrl = S_URL . '/Api/NotifyCallback/YimatongNotify';
        $order_sn = $order_info['ordernumber'];
        $price = $order_info['paymoney'];
        $post_data = array(
            'data' => array(
                "shopid" => $merchantCode,
                "subid" => $this->config['appid'],
                "MoneyTotal" => $price,
                'orderno' => $order_sn,
                'game_id' => $order_sn,
                'attach' => ''
            ),
        );
        // 数据安全加密传到attach字段
        $get_string = '';
        foreach ($post_data['data'] as $key1 => $val) {
            if ($key1 == 'attach') continue;
            $get_string .= $key1 . "=" . $val . "&";
        }
        $get_string = rtrim($get_string, '&');
        $attach = strtoupper(md5($get_string . 'i have Dream1573606091'));
        // shopid=35045&subid=800001&MoneyTotal=0.01&orderno=ChangbaoPay20171110223027809743&game_usid=ChangbaoPay20171110223027809743
        $post_data['data']['attach'] = $attach;
        $json = json_encode($post_data);
        $Send_url = 'http://pay.bests.so/service/JsPay';
        $arr = $this->http_post_json($Send_url, $json);
        $res = json_decode($arr[1], true);
        if ($res['data'] != '') {
            return $res['data'];
        } else {
            $ret = '';
        }
        return $ret;
    }


    private function http_post_json($url, $jsonStr)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json; charset=utf-8',
                'Content-Length: ' . strlen($jsonStr)
            )
        );
        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        return array($httpCode, $response);
    }

    private function setSign($data = array())
    {
        // 拼接签名字符串
        $str = "";
        $data = array(
            "shopid" => $this->config['mch_id'],
            "subid" => $this->config['appid'],
            "MoneyTotal" => $data['total_fee'] / 100,
            'orderno' => $data['game_id'],
            'game_usid' => $data['game_id'],
        );
        foreach ($data as $key1 => $val) {
            if ($val != '') $str = $str . $key1 . '=' . $val . '&';
        }
        $signTmpStr = rtrim($str, '&'); // 待加密的字符串
        $sign = strtoupper(md5($signTmpStr . 'i have Dream1573606091'));
        return $sign;
    }

    //回调方法，把返回数据处理成统一规定返回
    public function response()
    {
        $data = json_decode(file_get_contents('php://input'), true);
        $Sign = $data['attach'];
        $Signx = $data['sign'];

        // 进行签名验证
        $MySign = $this->setSign($data);
        $data['notify_url'] = 'http://www.1diantv.com/Api/NotifyCallback/YimatongNotify';
//        file_put_contents('./sign.txt' , var_export($Signx , true));
        $str = "attach=" . $data['attach'] . '&merchant_no=' . $data['merchant_no'] . '&notify_url=' . $data['notify_url'] . '&terminal_trace=' . $data['terminal_trace'] . '&total_fee=' . $data['total_fee'] . '&game_id=' . $data['game_id'] . '&channel_trade_no=' . $data['channel_trade_no'];
        $mysignx = md5($str . "&access_token=" . $this->config["mch_key"]);
        if ($mysignx != $Signx) {
            // 易码通的签名
            return false;
        }

        if ($data['return_code'] == '01' || $data['result_code'] == '01') {
            return true;
        } else {
            return false;
        }

    }
}
